dofile ("libColor.lua")
dofile ("StylusBox.lua")

Button = {}

bxx, bxy = 0, 0

function Button.init (bu, X, Y, Long, Haut, texte)
  _G[bu] = {}
  
  _G[bu].scr = SCREEN_DOWN
  
  _G[bu].x = X
  _G[bu].y = Y
  
  _G[bu].x2 = X + Long
  _G[bu].y2 = Y + Haut
  
  _G[bu].txt = texte
  _G[bu].txtx = (_G[bu].x+Long/2)-((string.len (_G[bu].txt)*6)/2)
  _G[bu].txty = (_G[bu].y+Haut/2)-(9/2)
  
  _G[bu].c_1 = c_griswin
  _G[bu]. c_2 = c_noir
  
  _G[bu].box = newBox (_G[bu].x, _G[bu].y, _G[bu].x2, _G[bu].y2)
end

function Button.draw (bu)  
  if Stylus.held then
    bxx = Stylus.X
    bxy = Stylus.Y
  end
  
  screen.drawRect (_G[bu].scr, _G[bu].x, _G[bu].y, _G[bu].x2, _G[bu].y2, c_gris)
  screen.drawRect (_G[bu].scr, _G[bu].x+1, _G[bu].y+1, _G[bu].x2-1, _G[bu].y2-1, _G[bu].c_1)
  screen.drawLine (_G[bu].scr, _G[bu].x+2, _G[bu].y2-2, _G[bu].x2-1, _G[bu].y2-2, _G[bu].c_2)
  screen.drawLine (_G[bu].scr, _G[bu].x2-2, _G[bu].y+2, _G[bu].x2-2, _G[bu].y2-2, _G[bu].c_2)
  screen.drawFillRect (_G[bu].scr, _G[bu].x+2, _G[bu].y+2, _G[bu].x2-2, _G[bu].y2-2, c_gris)
  screen.print (_G[bu].scr, _G[bu].txtx, _G[bu].txty, _G[bu].txt, c_noir)
    
  if Stylus.heldinBox (_G[bu].box, bxx, bxy) then
    _G[bu].c_1 = c_noir
    _G[bu].c_2 = c_griswin
  else 
    _G[bu].c_1 = c_griswin
    _G[bu].c_2 = c_noir
  end
end

function Button.clicked (bu)
  local clicked = false
  
  if Stylus.newPressinBox (_G[bu].box, bxx, bxy) then
    clicked = true
  end
  
  return clicked
end

function Button.clear (bu)
  _G[bu] = nil
  
  bxx = nil
  bxy = nil
end

